<?php
class Product{
    public $name;
    public $price;

    public function __construct($name,$price)
    {
        $this->name = $name;
        $this->price = $price;
    }
}
class ProcessSale{
    private $callBacks;

    public function registerCallback($callBack){
        if(!is_callable($callBack)){
            throw new Exception('方法不能被调用');
        }
        $this->callBacks[] = $callBack;
    }

    public function sale($product){
        /*echo $product->name;*/
        foreach($this->callBacks as $callBack){
            call_user_func($callBack,$product);
        }
    }
}


/*//简单匿名函数调用
$callback = create_function('$product','echo "$product->name";');
//第二种匿名函数
$callback = function($product){
    echo $product->name;
};

$processSale = new ProcessSale();
$processSale->registerCallback($callback);

$processSale->sale(new Product('衣服',6));

$processSale->sale(new Product('咖啡',3));*/

class Totalizer{
    static public function warnAmount($amt){
        $count = 0;
        return function($product) use ($amt,&$count){
            $count += $product->price;
            echo $product->name;
            if($count>$amt){
                echo '大于了输入的金额';
            }
        };
    }
}

$processSale = new ProcessSale();
$processSale->registerCallback(Totalizer::warnAmount(8));

$processSale->sale(new Product('衣服',1));

$processSale->sale(new Product('咖啡',3));